<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-position="right">
      <el-form-item label="群组信息">
        <el-link type="success" disabled>{{this.queryParams.title}}</el-link>
        <el-link type="primary" disabled>{{form.chatId}}</el-link>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.bot_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <line-button ref="lineButton" label="欢迎语" placeholder="请输入群欢迎语" :data="form.config"></line-button>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { getWelcome, updateWelcome } from "@/api/bot/welcome";
import {sendMessageTest } from "@/api/bot/botinfo";
import lineButton from '@/components/bot/inline_button.vue'
export default {
  name: "Welcome",
  dicts: ['bot_status'],
  components: { lineButton },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入群欢迎表格数据
      welcomeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        chatId: null,
        title:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        chatId: [
          { required: true, message: "群组ID不能为空", trigger: "change" }
        ],
        config: [
          { required: true, message: "配置不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams = this.$route.query
    this.handleUpdate()
  },
  methods: {
    //返回上个页面
    back(){
      this.$tab.closeOpenPage({ path: "/tg/group_client" });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chatId:this.queryParams.chatId,
        title:this.queryParams.title,
        config: {
          chatId: this.queryParams.chatId,
          content:"",
          buttonRows: [
            []
          ],
        },
        remark: null,
        orderNum: null,
        status: '1',
        delFlag: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      getWelcome(this.queryParams.chatId).then(response => {
        if(response.data){
          response.data.config = JSON.parse(response.data.config)
          //防止群变更时候配置里面的ID不变
          response.data.config.chatId = response.data.chatId
          this.form = response.data;
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      let validateEmpty = this.$refs.lineButton.validateEmpty();
      if(!validateEmpty.validate){
        this.$modal.msgWarning(validateEmpty.msg);
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateWelcome(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.back()
          });
        }
      });
    },
  }
};
</script>
